/******************************************************************************

    Copyright (C) 2006 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.datatypes;

/** A string of characters. */
public class CharString extends AbstractTerm {
	
	private static final long serialVersionUID = 1L;

	private String string;

	/** Creates a new CharString. */
	public CharString(String s) {
		string = s;
	}

	/** Returns the contents of this string as an instance of String. */
	public String getString() {
		return string;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(string);
		int ind;
		for (ind = 0; ind < sb.length(); ind++)
			if (sb.charAt(ind) == '\\') {
				sb.replace(ind, ind + 1, "\\\\");
				ind++;
			}
		for (ind = 0; ind < sb.length(); ind++)
			if (sb.charAt(ind) == '\'') {
				sb.replace(ind, ind + 1, "\\\'");
				ind++;
			}
		return "'" + sb + "\'";
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (obj instanceof CharString)
			return string.equals(((CharString) obj).string);
		if (obj instanceof String)
			return obj.equals(string);
		return false;
	}

	public int hashCode() {
		return string.hashCode();
	}

}